class Timer{
  constructor(timer, relayTimes){
    this.timer = timer;
    this.relayTimes = relayTimes;
    this.inteval;
    this.callback;
  }
  schdule(callback){
    if (this.inteval==null){
      this.callback = callback;
      this.inteval = setInterval(() => {
        if (Date.now() - this.timer > this.relayTimes) {
          try{
            this.callback();
            this.timer = Date.now();
          }catch(e){
            clearInterval(this.inteval);
            throw e;
          }
        }
      }, 100)
    }
  }
  restart(){
    this.timer = Date.now();
  }
  close(){
    clearInterval(this.inteval);
  }
  nowSchdule(){
    this.timer = Date.now();
    this.callback();
  }
}

module.exports = Timer;